<?php
// +------------------------------------------------------------------------------	
//	@desc adminModel for User
//  @date Wed Aug 19 CST 2009
//  @author Fly Jin <flysharping@gamil.com>
// +------------------------------------------------------------------------------

class Admin extends Model{
	public 	$db;
	private $tab_user;
	
	function __construct(){
		$this->tab_user       = T . 'user';
	}
	function login($username,$password){
		$sql="SELECT  id FROM $this->tab_user WHERE username='$username' AND password=MD5('$password')";
		$rs= $this->db->fetchRowOne($sql);
		if ($rs['id']>0) {
			$_SESSION['username']	=$username;
			$_SESSION['userid']		=$rs['id'];
			return true;
		}else{
			return false;
		}
	}
	
	function logout(){
		$_SESSION = array();
		session_destroy();
	}
	
	function isLogin() {
		if(!empty($_SESSION['username']))
			return 1;	
		else
			return 0;
	}
	
	function setPwd($oldPwd, $newPwd){
		$sql = "SELECT COUNT(id) AS id FROM $this->tab_user WHERE password= MD5('$oldPwd')";
		$rs= $this->db->fetchRowOne($sql);
		if($rs['id'] == 0){
			return 2;	
		}
		$row=array('password'=>md5($newPwd),);
		if ($this->db->update($this->tab_user,$row,'id='.$_SESSION['userid'])) {
			return 1;
		}
		/*$sql = "UPDATE $this->tab_user SET password = MD5('$newPwd') WHERE id =".$_SESSION['userid'];
		if ($this->db->query($sql))
			return 1;*/
		else
			return 0;
	}
	
}
?>